package com.getui.demo;

import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.RequiresApi;
import android.util.Log;
import com.igexin.sdk.PushManager;
import java.util.Timer;
import java.util.TimerTask;

public class ForegroundService extends Service {


    private NotificationManager notificationManager;
    private final static int GRAY_SERVICE_ID = 1001;
    private String notificationId = "channelId";
    private String notificationName = "channelName";
    private int notifyId = 0;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("ForegroundService", "onCreate");

    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        NotificationChannel channel = new NotificationChannel(notificationId, notificationName, NotificationManager.IMPORTANCE_HIGH);
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);


        Notification notification = new Notification.Builder(this, notificationId)
                .setContentTitle("正在接收消息")
                .setSmallIcon(R.drawable.push_small)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.push))
                .build();
        startForeground(GRAY_SERVICE_ID, notification);

        final Notification.Builder builder = new Notification.Builder(this, notificationId)
                .setContentTitle("!isRunService")
                .setSmallIcon(R.drawable.push_small)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.push_small));



        Log.d("ForegroundService", "onStartCommand");
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (!isRunService(getApplicationContext(), "com.getui.demo.DemoIntentService")) {
                    Log.d("ForegroundService", "!isRunService(GeTuiIntentService)");

                    startService(new Intent(getApplicationContext(),DemoIntentService.class));

                }
                PushManager pushManager = PushManager.getInstance();
                boolean isTurnOn = pushManager.isPushTurnedOn(getApplicationContext());
                if (!isTurnOn){
                    Log.d("ForegroundService", "pushManager.isPushTurnedOff");
                    pushManager.turnOnPush(getApplicationContext());
                    //测试代码
/*                    builder.setContentText("pushManager.isPushTurnedOff");
                    notificationManager.notify(notifyId++,builder.build());*/
                }
            }
        }, 0, 5 * 1000);


        return super.onStartCommand(intent, flags, startId);
    }


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    public boolean isRunService(Context context, String serviceName) {
        ActivityManager manager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceName.equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

}
